﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Skyhook.Core;

namespace Skyhook.Isometry
{
    internal class WorldObject
    {
        WorldPosition Position = null;
        WorldObjectContent pContent = null;
              
        event WorldObjectTilePositionChangeEvent OnTileChange = null;
        event WorldObjectDeleteEvent OnDelete = null;

        int WorldWidth;
        int WorldHeight;

        public WorldObject(WorldObjectTilePositionChangeEvent ontilechange, WorldObjectDeleteEvent ondelete,
            int worldwidth, int worldheight)
        {
            Position = new WorldPosition(this.OnPut,this.OnMove);
            OnTileChange += ontilechange;
            OnDelete += ondelete;

            WorldWidth = worldwidth;
            WorldHeight = worldheight;
        }

        public void OnPut(Vector2 oldposition)
        {
            int oldx = (int)oldposition.X;
            int oldy = (int)oldposition.Y;

            ExecuteOnTileChange(oldx, oldy);
        }

        public void OnMove(Vector2 oldposition)
        {
            if (Position.X < 0.5f )
            {
                Position.DisableEvents();
                Position.Put(0.5f,Position.Y);
                Position.EnableEvents();
            }
            if (Position.X > WorldWidth - 0.5f)
            {
                Position.DisableEvents();
                Position.Put(WorldWidth - 0.5f, Position.Y);
                Position.EnableEvents();
            }
            if (Position.Y < 0.5f)
            {
                Position.DisableEvents();
                Position.Put(Position.X,0.5f);
                Position.EnableEvents();
            }
            if(Position.Y > WorldHeight - 0.5f)
            {
                Position.DisableEvents();
                Position.Put(Position.X,WorldHeight - 0.5f);
                Position.EnableEvents();
            }

            int oldx = (int)oldposition.X;
            int oldy = (int)oldposition.Y;

            if (TileChanged(oldx, oldy))
            {
                ExecuteOnTileChange(oldx, oldy);
            }
        }

        private bool TileChanged(int oldx, int oldy)
        {
            return (oldx != (int)Position.X || oldy != (int)Position.Y);
        }

        private void ExecuteOnTileChange(int oldx, int oldy)
        {
            if (OnTileChange != null)
            {
                OnTileChange(this, Position, new Point(oldx, oldy));
            }
        }

        private void ExecuteOnDelete()
        {
            if (OnDelete != null)
            {
                OnDelete(this);
            }
        }

        public WorldObjectController CreateController()
        {
            return new WorldObjectController(Position, ExecuteOnDelete);
        }

        public WorldScreenPosition CreateScreenPosition()
        {
            return new WorldScreenPosition(Position);
        }

        public void InsertContent(ILogic logic, IVisual visual)
        {
            pContent = new WorldObjectContent(logic,visual);
        }

        public virtual WorldObjectContent Content
        {
            get
            {
                return pContent;
            }
        }
    }
}
